package com.szy.bigevent.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
public class AsyncConfig {

    @Bean(name = "fileProcessingExecutor")
    public Executor fileProcessingExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(5);
        // 最大线程数
        executor.setMaxPoolSize(10);
        // 队列容量
        executor.setQueueCapacity(25);
        // 线程名称前缀
        executor.setThreadNamePrefix("FileProcessor-");
        // 当线程池达到最大线程数时如何处理新任务
        executor.setRejectedExecutionHandler((r, executor1) -> {
            // 可以根据需要自定义处理逻辑
            throw new RuntimeException("文件处理任务队列已满，请稍后再试");
        });
        executor.initialize();
        return executor;
    }

}
